package command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CommandLinux implements Command{
    private static final CommandLinux commandLinux;
    static {
        commandLinux = new CommandLinux();
    }
    @Override
    public String oSCommand(String[] args) throws IOException {
        String output = "";
        Process process = Runtime.getRuntime().exec(args);
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line ;
        while ((line = errorReader.readLine()) != null) {
            output+=line+System.getProperty("line.separator");
        }
        errorReader.close();
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((line = infoReader.readLine()) != null) {
            output+=line+System.getProperty("line.separator");
        }
        infoReader.close();
        System.out.println(output);
        return output;
    }

    public static String oSCommandStatic(String[] args) throws IOException{
        return commandLinux.oSCommand(args);
    }

    private static void print(String[] args) {
        for (String arg : args) {
            System.out.println(arg);
            System.out.print(" ");
        }
    }

    private static void print(String arg) {
        System.out.println(arg);
    }
}
